<?php
/**
 * 资源加载类
 */

class Lin_Loader
{	
	/*
	 * 加载文件
	 */
	static public function loadFile($fileName, $dirs = null, $once = false)
	{
		if ($dirs === null)
		{
			if (self::isInclude($fileName, '', $once))
			{
				return true;
			}
		}
		elseif (is_array($dirs))
		{
			foreach ($dirs as $val)
			{
				if (self::isInclude($fileName,  $val, $once))
				{
					return true;
				}
			}
		}
		else{
				if (self::isInclude($fileName,  $dirs, $once))
				{
					return true;
				}			
		}
		
		return false;
	}
	
	/*
	 * 加载类
	 */
	static public function loadClass($class, $dirs)
	{
		$tmp = self::_getPathByClass($class);
		if (!is_array($dirs))
		{
			if (self::isInclude($tmp['class'],  $dirs . "/" . $tmp['dir'], true))
			{
				return true;
			}
		}
		else
		{
			foreach ($dirs as $d)
			{
				if (self::isInclude($tmp['class'],  $d . "/" . $tmp['dir'], true))
				{
					return true;
				}
			}
		}
		return false;
	}
	
	static private function _getPathByClass($class)
	{
		$return = array();
		$return['class'] = '';
		$return['dir'] = '';
		$tmp = strripos($class, '_');
		if ($tmp)
		{
			$return['class'] = substr($class, $tmp+1, strlen($class) - 1);
			$return['dir'] = str_replace('_', '/', substr($class, 0, $tmp));
			return $return;
		}
		else 
		{
			$return['class'] = $class;
			return $return;	
		}
	}
	
	static private function isInclude($fileName, $dirs = '', $once = false)
	{
		$path = get_include_path();
		$tmp = explode(PATH_SEPARATOR, $path);
		
		$file = "{$dirs}/{$fileName}.php";
		if (realpath($file))
		{
			self::_include($file, $once);
			return true;
		}
		
		foreach ($tmp as $val)
		{
			$file = "{$val}/{$dirs}/{$fileName}.php";
			if (realpath($file))
			{
				self::_include($file, $once);
				return true;		
			}		
		}
		
		return false;
	}
	
	static private function _include($file, $once = false)
	{
		if ($once)
		{
			include_once $file;
		}
		else
		{
			include $file;
		}
	}
	
	static public function validation()
	{
		$html = '<link rel="stylesheet" href="/css/validationEngine.jquery.css" type="text/css"/>
			<script src="/js/validation/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
			<script src="/js/validation/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>';
		return $html;
	}
		
}